package com.example.websocketclusterspringbootstarter.utils;

import com.corundumstudio.socketio.SocketIOClient;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 本地缓存工具类
 */
public class LocalCacheUtil {


    private LocalCacheUtil() {
    }

    private static final ConcurrentHashMap<String, SocketIOClient> MAP = new ConcurrentHashMap<>();

    /**
     * 添加本地客户端
     *
     * @param sessionId 会话sessionId
     * @param client    ws会话客户端
     */
    public static void addLocalClient(String sessionId, SocketIOClient client) {
        MAP.put(sessionId, client);
    }

    /**
     * 根据sessionId获取本地客户端
     *
     * @param sessionId 会话sessionId
     * @return SocketIOClient  ws会话客户端
     */
    public static SocketIOClient getLocalClient(String sessionId) {
        return MAP.get(sessionId);
    }

    /**
     * 根据sessionId获取移除本地客户端
     *
     * @param sessionId 会话sessionId
     */
    public static void removeClient(String sessionId) {
        MAP.remove(sessionId);
    }

}
